/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package capturador;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author alejandro
 */
public class LienzoUmbralizado extends javax.swing.JPanel {

    private int umbral;
    private BufferedImage iOriginal;
    private BufferedImage iUmbralizado;
    private LienzoOriginal lienzoOriginal;
    
    public LienzoUmbralizado() {
        initComponents();
        this.setUmbral(127);
        try {
           this.setiOriginal(this.getLienzoOriginal().getImgGrises()); 
        } 
        catch (Exception NullPointerException) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void paint( Graphics g ) {
        super.paint( g );
        //Se crea la imagen umbralizada
        if(this.getiOriginal()!=null)
        {
            this.setiUmbralizado(
                                    new BufferedImage   (
                                                        this.getiOriginal().getWidth(),
                                                        this.getiOriginal().getHeight(),
                                                        BufferedImage.TYPE_INT_RGB
                                                        )
                                    );
            int c=0;
            int gris;
            int ngris=0;
            for (int y = 0; y < this.getiOriginal().getHeight(); y++) {  
                for (int x = 0; x < this.getiOriginal().getWidth(); x++) { 
                    c = this.getiOriginal().getRGB(x,y);
                    int  red = (  c & 0x00FF0000) >> 16;  
                    int  green = (c & 0x0000FF00) >> 8;  
                    int  blue =   c & 0x000000FF;
                    gris = (int) (.299 * red + .587 * green + .114 * blue);

                    //aplicando el umbral
                    if(gris< umbral) ngris=0xffffffff;
                    
                    //pintando el valor
                    this.getiUmbralizado().setRGB(x,y,ngris);
                    ngris=0;
                }
            }
            //se obtiene la instancia Graphics2D sobre el nuevo BufferedImage en tonos de gris
            Graphics2D graficadorGrises = this.getiUmbralizado().createGraphics();
            //Se grafica
            graficadorGrises.drawImage(this.getiUmbralizado(),0,0,this.getWidth(),this.getHeight(),null);
            g.drawImage(this.getiUmbralizado(), 0, 0, this);
        }
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    /**
     * @return the umbral
     */
    public int getUmbral() {
        return umbral;
    }

    /**
     * @param umbral the umbral to set
     */
    public void setUmbral(int umbral) {
        this.umbral = umbral;
    }

    /**
     * @return the iOriginal
     */
    public BufferedImage getiOriginal() {
        return iOriginal;
    }

    /**
     * @param iOriginal the iOriginal to set
     */
    public void setiOriginal(BufferedImage iOriginal) {
        this.iOriginal = iOriginal;
    }

    /**
     * @return the iUmbralizado
     */
    public BufferedImage getiUmbralizado() {
        return iUmbralizado;
    }

    /**
     * @param iUmbralizado the iUmbralizado to set
     */
    public void setiUmbralizado(BufferedImage iUmbralizado) {
        this.iUmbralizado = iUmbralizado;
    }

    /**
     * @return the lienzoOriginal
     */
    public LienzoOriginal getLienzoOriginal() {
        return lienzoOriginal;
    }

    /**
     * @param lienzoOriginal the lienzoOriginal to set
     */
    public void setLienzoOriginal(LienzoOriginal lienzoOriginal) {
        this.lienzoOriginal = lienzoOriginal;
        this.iOriginal = this.getLienzoOriginal().getImgOriginal();
        repaint();
    }
}
